package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.powermaster.proto.GetLogHistoryRequest;
import com.aliyun.heiyu.powermaster.server.dao.LogHistoryDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.LogHistoryEntity;
import java.util.Date;
import java.util.List;


public class LogHistoryImpl implements LogHistoryDao {

    @Override
    public Integer saveLogHistory(LogHistoryEntity entity) {
        String sql = "INSERT INTO log_history (root_level, `level`, log_size, log_num, monitorInterval, create_time, log_switch) VALUES(?, ?, ?, ?, ?, ?, ?)";
        Object[] params = new Object[7];
        params[0] = entity.getRoot_level();
        params[1] = entity.getLevel();
        params[2] = entity.getLog_size();
        params[3] = entity.getLog_num();
        params[4] = entity.getMonitorInterval();
        params[5] = new Date();
        params[6] = entity.getLog_switch();
        return JdbcUtil.insert(sql, params);
    }

    @Override
    public List<LogHistoryEntity> getLogHistory(GetLogHistoryRequest request) {
        String sql = "select * from log_history where 1=1 ";
        if(!StringUtil.isEmpty(request.getStartTime()) && !StringUtil.isEmpty(request.getEndTime())){
            Object[] params = new Object[2];
            params[0] = request.getStartTime();
            params[1] = request.getEndTime();
            return JdbcUtil.queryForList(sql + " and create_time between ? and ? order by id desc", params, LogHistoryEntity.class);
        }
        return JdbcUtil.queryForList(sql+ "order by id desc", null, LogHistoryEntity.class);
    }


    @Override
    public LogHistoryEntity selectLast() {
        String sql = "select * from log_history where 1=1 order by id desc limit 1";
        return JdbcUtil.queryForBean(sql, null, LogHistoryEntity.class);
    }
}
